package com.example.administrator.mobliesafe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.mobilesafe.service.PhoneAddressService;
import com.example.administrator.mobilesafe.ui.SettingClickView;
import com.example.administrator.mobilesafe.ui.SettingItemView;
import com.example.administrator.mobilesafe.utils.CheckService;

public class SettingActivity extends Activity {
    private SettingItemView siv_update;
    private SharedPreferences sharedPreferences;
    private SettingItemView siv_query_address;
    private SettingClickView scv_choose_color;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        sharedPreferences = getSharedPreferences("config",MODE_PRIVATE);
        boolean update = sharedPreferences.getBoolean("update",false);
        boolean queryAddress = sharedPreferences.getBoolean("query_address",false);
        boolean queryIsRunning = CheckService.isServiceRunning(SettingActivity.this,"com.example.administrator.mobilesafe.service.PhoneAddressService");
        siv_update = (SettingItemView) findViewById(R.id.siv_update);
        siv_query_address = (SettingItemView) findViewById(R.id.query_number_address);
        scv_choose_color =(SettingClickView)findViewById(R.id.scv_changebg);
        if(update){
            siv_update.setChecked(true);
        }else {
            siv_update.setChecked(false);
        }
        siv_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                if(siv_update.isChecked()){

                    siv_update.setChecked(false);
                    editor.putBoolean("update", false);
                }else {
                    siv_update.setChecked(true);
                    editor.putBoolean("update", true);
                }
                editor.commit();
            }
        });

        final Intent showAddress = new Intent(this, PhoneAddressService.class);
        if (queryAddress){
            if(!queryIsRunning)
                startService(showAddress);
            siv_query_address.setChecked(true);
        }else {
            if (queryIsRunning)
                stopService(showAddress);
            siv_query_address.setChecked(false);
        }
        siv_query_address.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                if(siv_query_address.isChecked()){
                    siv_query_address.setChecked(false);
                    stopService(showAddress);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean("query_address",false);
                    editor.commit();
                }else {
                    siv_query_address.setChecked(true);
                    startService(showAddress);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean("query_address", true);
                    editor.commit();
                }
            }
        });
        scv_choose_color = (SettingClickView) findViewById(R.id.scv_changebg);
        scv_choose_color.setTitle("归属地提示框风格");
        final String [] items = {"半透明","活力橙","卫士蓝","金属灰","苹果绿"};
        int which = sharedPreferences.getInt("which", 0);
        scv_choose_color.setDesc(items[which]);

        scv_choose_color.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int dd = sharedPreferences.getInt("which", 0);
                // 弹出一个对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                builder.setTitle("归属地提示框风格");
                builder.setSingleChoiceItems(items,dd, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        //保存选择参数
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putInt("which", which);
                        editor.commit();
                        scv_choose_color.setDesc(items[which]);

                        //取消对话框
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("cancel", null);
                builder.show();

            }
        });

    }


}
